package com.digua.base.socket.util

import android.os.Handler
import android.os.Looper


class ThreadUtil {
    companion object {
        private var sMainHandler: Handler? = null

        /**
         * 是否为主线程
         */
        fun checkMainThread() = Looper.getMainLooper() == Looper.myLooper()

        /**
         * 将 Runnable 运行在主线程
         */
        fun runOnMainThread(runnable: Runnable) {
            checkMainHandlerIsNull()
            sMainHandler?.post(runnable)
        }

        private fun checkMainHandlerIsNull() {
            if (sMainHandler == null) sMainHandler = Handler(Looper.getMainLooper())
        }
    }
}